let logger = require("pomelo-logger").getLogger(__filename);

let mongodb = require("mongodb");
let path = require("path");

module.exports = function (app) {
    return function () {
        initialize(app);
    };
};

function initialize(app) {
    app.set('proxyConfig', {
        retryTimes: -1
    });

    app.loadConfig("dbConfig", path.join(app.getBase(), "/config/db.json"));
    app.loadConfig("gameConfig", path.join(app.getBase(), "/config/games.json"));
    app.loadConfig("accountConfig", path.join(app.getBase(), "/config/account.json"));
    app.loadConfig("businessConfig", path.join(app.getBase(), "/config/business.json"));
    app.loadConfig("amqpConfig", path.join(app.getBase(), "/config/amqp.json"));
    app.filter(require("./filter/accountFilter")());
    app.filter(require("./filter/debugFilter")());
    app.filter(require("./filter/switchFilter")());

    let dbConfig = app.get("dbConfig");
    let gameConfig = app.get("gameConfig");

    mongodb.MongoClient.connect(dbConfig["mongo_game"], function (error, db) {
        if (!!error) {
            logger.error("connect to mongo_game failed. mongoUrl=" + dbConfig["mongo_game"] + " error=" + error);
            return;
        }
        logger.info("connect to mongo_game success");
        app.set("gameDBClient", db);

        let playerDao = require(path.join(app.getBase(), "/app/dao/playerDao")).create(app);
        app.set("playerDao", playerDao);

        let mailDataDao = require(path.join(app.getBase(), "/app/dao/mailDataDao")).create(app);
        app.set("mailDataDao", mailDataDao);

        let gameRecordDao = require(path.join(app.getBase(), "/app/dao/gameRecordDao")).create(app);
        app.set("gameRecordDao", gameRecordDao);

        let gameResultDao = require(path.join(app.getBase(), "/app/dao/gameResultDao")).create(app);
        app.set("gameResultDao", gameResultDao);

        let swiperItemDao = require(path.join(app.getBase(),"/app/dao/swiperItemDao")).create(app);
        app.set("swiperItemDao", swiperItemDao);

        let activityDao = require(path.join(app.getBase(),"/app/dao/activityDao")).create(app);
        app.set("activityDao", activityDao);

        let headlineDao = require(path.join(app.getBase(),"/app/dao/headlineDao")).create(app);
        app.set("headlineDao", headlineDao);

        let configDao = require(path.join(app.getBase(),"/app/dao/configDao")).create(app);
        app.set("configDao", configDao);

        let announcementDao = require(path.join(app.getBase(),"/app/dao/announcementDao")).create(app);
        app.set("announcementDao", announcementDao);

        let orderDao = require(path.join(app.getBase(),"/app/dao/orderDao")).create(app);
        app.set("orderDao", orderDao);

        let configService = require(path.join(app.getBase(), "./app/domain/services/configService")).create(app);
        app.set("configService", configService);
        configService.initialize();


        let amqpService = require(path.join(app.getBase(), "./app/domain/services/amqpService")).create(app);
        app.set("amqpService", amqpService);

        let pushMessageService = require(path.join(app.getBase(), "/app/domain/services/pushMessageService")).create(app);
        app.set('pushMessageService', pushMessageService);

        let playerService = require(path.join(app.getBase(), "/app/domain/services/playerService")).create(app);
        app.set("playerService", playerService);
        playerService.initialize();

        let mailDataService = require(path.join(app.getBase(), "/app/domain/services/mailDataService")).create(app);
        app.set("mailDataService", mailDataService);
        mailDataService.initialize();

        let gameService = require(path.join(app.getBase(), "/app/domain/services/gameService")).create(app);
        app.set("gameService", gameService);
        gameService.initialize(gameConfig);

        let headlineService = require(path.join(app.getBase(), "/app/domain/services/headlineService")).create(app);
        app.set("headlineService", headlineService);
        headlineService.initialize();

        let leaderBoardService = require(path.join(app.getBase(), "./app/domain/services/leaderBoardService")).create(app);
        app.set("leaderBoardService", leaderBoardService);

        let gameRecordService = require(path.join(app.getBase(), "./app/domain/services/gameRecordService")).create(app);
        app.set("gameRecordService", gameRecordService);

        let gameResultService = require(path.join(app.getBase(), "./app/domain/services/gameResultService")).create(app);
        app.set("gameResultService", gameResultService);

        let swiperItemService = require(path.join(app.getBase(), "./app/domain/services/swiperItemService")).create(app);
        app.set("swiperItemService", swiperItemService);
        swiperItemService.initialize();

        let activityService = require(path.join(app.getBase(), "./app/domain/services/activityService")).create(app);
        app.set("activityService", activityService);
        activityService.initialize();

        let announcementService = require(path.join(app.getBase(), "./app/domain/services/announcementService")).create(app);
        app.set("announcementService", announcementService);
        announcementService.initialize();
    });
}